package com.test.sharding.algorithm;

import io.shardingsphere.core.api.algorithm.sharding.PreciseShardingValue;
import io.shardingsphere.core.api.algorithm.sharding.standard.PreciseShardingAlgorithm;

import java.util.Collection;

/**
 * 精确分片算法类名称，用于=和IN
 * 该类需实现PreciseShardingAlgorithm接口并提供无参数的构造器
 *
 */
public class ValueOneDatasourcePreciseAlgorithm implements PreciseShardingAlgorithm {
    public String doSharding(Collection availableTargetNames, PreciseShardingValue shardingValue) {
        String[] dataSourceNames = new String[availableTargetNames.size()];
        availableTargetNames.toArray(dataSourceNames);

        String type = String.valueOf(Long.valueOf(shardingValue.getValue().toString()) % 3);

        for (String dataSourceName : dataSourceNames) {
            if (dataSourceName.endsWith(type)) {
                return dataSourceName;
            }
        }

        return null;
    }
}
